package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.*;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.*;
import com.atguigu.jxc.service.GoodsService;
import com.atguigu.jxc.service.LogService;
import com.atguigu.jxc.service.ReturnListGoodsService;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Type;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description
 */
@Service
public class ReturnListGoodsServiceImpl implements ReturnListGoodsService {

    @Autowired
    private ReturnListGoodsDao returnListGoodsDao;

    @Autowired
    private ReturnListDao returnListDao;

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private GoodsTypeDao goodsTypeDao;

    /**
     * 保存退货单信息
     * @param returnList 退货单信息实体
     * @param returnListGoodsStr 退货商品信息JSON字符串
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(ReturnList returnList, String returnListGoodsStr) {
        Gson gson = new Gson();
        Type type = new TypeToken<List<ReturnListGoods>>() {}.getType();
        List<ReturnListGoods> ReturnListGoodsList = gson.fromJson(returnListGoodsStr, type);
        //保存退货单
        returnListDao.insertReturnList(returnList);
        //增加退货信息
        for (ReturnListGoods returnListGoods : ReturnListGoodsList) {
            returnListGoods.setReturnListId(returnList.getReturnListId());
            returnListGoodsDao.saveReturnListGoods(returnListGoods);
            goodsService.updateGoodsStock(returnListGoods.getGoodsId(), returnListGoods.getGoodsNum(), returnListGoods.getPrice(), returnList.getState(), 1);
        }
    }

    /**
     * 查询退货单
     * @param returnNumber 单号
     * @param supplierId 供应商ID
     * @param state 付款状态
     * @param sTime 开始时间
     * @param eTime 结束时间
     * @return
     */
    @Override
    public Map<String, Object> list(String returnNumber, Integer supplierId, Integer state, String sTime, String eTime) {
        Map<String,Object> result = new HashMap<>();

        List<ReturnList> returnListList = returnListDao.getReturnList(returnNumber, supplierId, state, sTime, eTime);

        result.put("rows", returnListList);

        return result;
    }

    /**
     * 查询退货单商品信息
     * @param returnListId 退货单ID
     * @return
     */
    @Override
    public Map<String, Object> goodsList(Integer returnListId) {
        Map<String,Object> map = new HashMap<>();
        List<ReturnListGoods> returnListGoodsList = returnListGoodsDao.getReturnListGoodsByReturnListId(returnListId);
        map.put("rows", returnListGoodsList);
        return map;
    }

    /**
     * 删除退货单及商品信息
     * @param returnListId 退货单ID
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Integer returnListId) {
        returnListGoodsDao.deleteReturnListGoodsByReturnListId(returnListId);
        returnListDao.deleteReturnListById(returnListId);
    }

    /**
     * 退货商品统计
     * @param sTime 开始时间
     * @param eTime 结束时间
     * @param goodsTypeId 商品类别ID
     * @param codeOrName 编号或商品名称
     * @return
     */
    @Override
    public String count(String sTime, String eTime, Integer goodsTypeId, String codeOrName) {
        JsonArray result = new JsonArray();
        List<ReturnList> purchaseListList = returnListDao.getReturnListByTime(sTime, eTime);
        for (ReturnList returnList : purchaseListList) {
            List<ReturnListGoods> purchaseListGoodsList = returnListGoodsDao.getReturnListGoodsByReturnListIdAndGoodsTypeIdAndCodeOrName(returnList.getReturnListId(), goodsTypeId, codeOrName);
            for (ReturnListGoods returnListGoods : purchaseListGoodsList) {
                JsonObject obj = new JsonObject();

                obj.addProperty("number", returnList.getReturnNumber());

                obj.addProperty("date", returnList.getReturnDate());

                obj.addProperty("supplierName", returnList.getSupplierName());

                obj.addProperty("code", returnListGoods.getGoodsCode());

                obj.addProperty("name", returnListGoods.getGoodsName());

                obj.addProperty("model", returnListGoods.getGoodsModel());

                obj.addProperty("goodsType", goodsTypeDao.getGoodsTypeById(returnListGoods.getGoodsTypeId()).getGoodsTypeName());

                obj.addProperty("unit", returnListGoods.getGoodsUnit());

                obj.addProperty("price", returnListGoods.getPrice());

                obj.addProperty("num", returnListGoods.getGoodsNum());

                obj.addProperty("total", returnListGoods.getTotal());

                result.add(obj);
            }
        }

        return result.toString();
    }

    /*



    @Override
    public ServiceVO delete(Integer returnListId) {

        logService.save(new Log(Log.DELETE_ACTION, "删除退货单："+returnListGoodsDao.getReturnList(returnListId).getReturnNumber()));

        returnListGoodsDao.deleteReturnListGoodsByReturnListId(returnListId);

        returnListGoodsDao.deleteReturnListById(returnListId);

        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    @Override
    public ServiceVO updateState(Integer returnListId) {

        returnListGoodsDao.updateState(returnListId);

        logService.save(new Log(Log.DELETE_ACTION, "支付结算退货单：" + returnListGoodsDao.getReturnList(returnListId).getReturnNumber()));

        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    @Override
    public String count(String sTime, String eTime, Integer goodsTypeId, String codeOrName) {

        JsonArray result = new JsonArray();

        try {

            List<ReturnList> returnListList = returnListGoodsDao.getReturnlist(null, null, null, sTime, eTime);

            for(ReturnList pl : returnListList){

                List<ReturnListGoods> returnListGoodsList = returnListGoodsDao
                        .getReturnListGoods(pl.getReturnListId(), goodsTypeId, codeOrName);

                for(ReturnListGoods pg : returnListGoodsList){

                    JsonObject obj = new JsonObject();

                    obj.addProperty("number", pl.getReturnNumber());

                    obj.addProperty("date", pl.getReturnDate());

                    obj.addProperty("supplierName", pl.getSupplierName());

                    obj.addProperty("code", pg.getGoodsCode());

                    obj.addProperty("name", pg.getGoodsName());

                    obj.addProperty("model", pg.getGoodsModel());

                    obj.addProperty("goodsType", goodsTypeDao.getGoodsTypeById(pg.getGoodsTypeId()).getGoodsTypeName());

                    obj.addProperty("unit", pg.getGoodsUnit());

                    obj.addProperty("price", pg.getPrice());

                    obj.addProperty("num", pg.getGoodsNum());

                    obj.addProperty("total", pg.getTotal());

                    result.add(obj);

                }
            }

            logService.save(new Log(Log.SELECT_ACTION, "退货商品统计查询"));

        } catch (Exception e) {

            e.printStackTrace();

        }

        return result.toString();
    }*/
}
